package xyz.weizhi;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import xyz.weizhi.mapper.BrandMapper;
import xyz.weizhi.pojo.Brand;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

/**
 * @Author milian
 * @Date 2021/8/22 0022 19:34
 */
public class mybatisTest {
    public static void main(String[] args) throws IOException {
        int id = 1;
        int status = 1;
        int[] ids = {7,8,9};
        String companyName = "华为";
        String brandName = "华为";
        // 处理参数
        companyName = "%"+companyName+"%";
        companyName = "%"+brandName+"%";
        String resource = "mybatis_config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        // 封装成Map
        HashMap map = new HashMap();
        map.put("status",status);
        map.put("companyName",companyName);
        map.put("brandName",brandName);
        // 封装成对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(200);
        brand.setStatus(0);
        // List<Brand> brands = getAllBrands(brandMapper);
        //List<Brand> brands = getBrandByID(id, brandMapper);
        //List<Brand> brands = getBrandByCondition(brandMapper, map);
        //List<Brand> brands = getBrandByConditionSingle(brandMapper, brand);
        //List<Brand> brands = updateBrandById(brandMapper, brand);
        // addBrand(brandMapper, brand);
        brandMapper.deleteByIds(ids);
        sqlSession.commit();
    }

    private static void addBrand(BrandMapper brandMapper, Brand brand) {
         brandMapper.addBrand(brand);
    }

    private static List<Brand> updateBrandById(BrandMapper brandMapper, Brand brand) {
        return brandMapper.updateById(brand);
    }

    private static List<Brand> getBrandByConditionSingle(BrandMapper brandMapper, Brand brand) {
        return brandMapper.selectByConditionSingle(brand);
    }

    private static List<Brand> getBrandByCondition(BrandMapper brandMapper, HashMap map) {
        return brandMapper.selectByCondition(map);
    }

    private static List<Brand> getBrandByID(int id, BrandMapper brandMapper) {
        return brandMapper.selectById(id);
    }

    private static List<Brand> getAllBrands(BrandMapper brandMapper) {
        List<Brand> brands = brandMapper.selectAll();
        return brands;
    }
}
